package geektime.io.nio2.demo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 通过FileChannel从特定位置读取某个文件
 */
public class FileChannelTest {
    public static void main(String[] args) throws IOException {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        FileChannel fileChannel = FileChannel.open(Paths.get("pom.xml"));
        fileChannel.read(byteBuffer, 300);
        System.out.println(new String(byteBuffer.array()));

        System.out.println("***********************");
        FileChannel fileChannel2 = (FileChannel) Files.newByteChannel(Paths.get("pom.xml"));
        byteBuffer.clear();
        fileChannel2.read(byteBuffer, 200);
        System.out.println(new String(byteBuffer.array()));

    }
}
